package Service;

import javax.transaction.Transactional;

import Algorithm.Search;
import Algorithm.SearchByTag;
import Algorithm.SearchImpl;
import Entities.DoeBookEntity;
import DAO.SearchDao;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@Transactional
public class SearchService
{
    private Search search;
    private SearchDao searchDao;
    private List<String> searchResult;  //这个列表中储存的元素格式是：图书id：评分
    private String user_name;
    private String searchResultKey;
    private Cache searchResultCache;

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public SearchDao getSearchDao() {
        return searchDao;
    }

    public void setSearchDao(SearchDao searchDao) {
        this.searchDao = searchDao;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public Cache getSearchResultCache() {
        return searchResultCache;
    }

    public void setSearchResultCache(Cache searchResultCache) {
        this.searchResultCache = searchResultCache;
    }

    //搜索函数，返回的搜索结果是Book实体的List
    public List<DoeBookEntity> search(String searchContent, String userName, int after_id, int limit, String session_Id, String searchResultKey)
    {
        if (searchResultKey.equals("searchResult"))
            setSearch(new SearchImpl());
        else if (searchResultKey.equals("searchResultByTag"))
            setSearch(new SearchByTag());
        searchResult = new ArrayList<>();
        user_name = userName;
        searchResultCache = CacheManager.getCacheManager("__DEFAULT__").getCache("SearchResultCache");
        String currentSearchResKey = searchResultKey +"_"+searchContent;

        if (searchResultCache.get(currentSearchResKey) == null)
        {
            searchResult = search.search(searchContent);  //返回图书id列表
            Element resElement = new Element(currentSearchResKey,searchResult);
            searchResultCache.put(resElement);
        }
        else
        {
            Element resultElement = searchResultCache.get(currentSearchResKey);
            searchResult = (List<String>) resultElement.getObjectValue();
        }


        List<String> ids = new ArrayList<>();
        List<String> score = new ArrayList<>();

        //searchResult is the final result
        ActionContext.getContext().getSession().put(currentSearchResKey,searchResult.size());
        if (searchResult.size() <= 0)
            return null;
        else {
            for (int i = 0;i<limit;i++){
                int index = i + after_id;
                if (index >= searchResult.size())
                    break;
                String result;
                result = searchResult.get(index);
                String [] bundle = result.split(":");
                ids.add(bundle[0]);
                score.add(bundle[1]);
            }
            return searchDao.bookListByIds(ids);
        }
    }//end function search()
}
